/**
 * @file
 * @auth jinguangguo
 * @date 2016/10/26
 */

import Vue from 'vue';
import './style.scss';
import moment from 'moment';
import Loading from '../../../grsc-base/component/Loading.vue';
import NoData from '../../../grsc-base/component/NoData.vue';
import commonService from '../../common/commonService';
import tpl from './tpl.html';
import service from './service';
import { PAGE_SIZE } from '../../../grsc-base/common/constants';
import Pager from '../../../grsc-base/component/Pager.vue';

export default Vue.extend({
    template: tpl,
    components: {
        loading: Loading,
        noData: NoData,
        Pager,
    },
    data() {
        return {
            moment: moment,
            noticeId: this.$route.params.noticeId,
            isLoading: false,
            email: '',
            detail: null,
            fundList: [],
            fundListlist: [],
            pls: [],
            bgs: [],
            isActive1: true,
            isActive2: false,
            isActive3: false,
            fundTitle: null,
            fundContent: null,
            param1: {
                pageNo: 1,
                pageSize: PAGE_SIZE,
                assetCode: "",
                type: 'FUND'
            },
            pageNum: 0,
            list: [],
        }
    },
    filters: {
        formatDate: function (value) {// 时间戳转换日期格式方法
            if (value == null) {
                return '';
            } else {
                let date = new Date(value);
                let y = date.getFullYear();// 年
                let MM = date.getMonth() + 1;// 月
                MM = MM < 10 ? ('0' + MM) : MM;
                let d = date.getDate();// 日
                d = d < 10 ? ('0' + d) : d;
                let h = date.getHours();// 时
                h = h < 10 ? ('0' + h) : h;
                let m = date.getMinutes();// 分
                m = m < 10 ? ('0' + m) : m;
                let s = date.getSeconds();// 秒
                s = s < 10 ? ('0' + s) : s;
                return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
            }
        }
    },
    computed: {

    },
    created() {
        this.fetchData();
    },
    methods: {
        //切换基金
        avtiveNotice(obj) {
            this.noticeId = obj.noticeId
            if (obj.content) {
                this.detail = obj.content;
                return false;
            }
            this.param1.assetCode = obj;
            this.getfundList()
        },

        /**
         * 获取数据
         */
        fetchData() {
            service.getConfigSymbolRank().done((rep) => {
                this.isLoading = false;
                if (rep.code == window.CODE.SUCCESS) {
                    if (this.$route.query.fund == "FUND") {
                        this.isActive1 = true;
                        this.isActive2 = false;
                        this.isActive3 = false;
                    }
                    if (this.$route.query.fund == "REPORT") {
                        this.isActive1 = false;
                        this.isActive2 = false;
                        this.isActive3 = true;
                    }
                    if (this.$route.query.fund == "PUBLISH") {
                        this.isActive1 = false;
                        this.isActive2 = true;
                        this.isActive3 = false;
                    }
                    if (this.$route.query.fund) {
                        this.param1.type = this.$route.query.fund
                    }
                    if (this.$route.query.type) {
                        this.param1.assetCode = this.$route.query.type.replace('USDT_','');
                    } else {
                        this.param1.assetCode = rep.data[0].assetCode2;
                    }
                    this.getfundList();
                    this.list = rep.data;
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        },

        getfundList() {
            let data = {
                symbol: this.param1.assetCode
            }
            service.fundList(data).done((rep) => {
                if (rep.data) {
                    this.fundListlist = rep.data;
                    this.pls = rep.data.pls;
                    this.bgs = rep.data.bgs;
                } else {
                    this.fundListlist = ''
                    this.pls = ''
                    this.bgs = ''
                }

            });
        },
        /**
        * 分页change
        * @param currentPage
        */
        changePage(currentPage) {
            this.param1.pageNo = currentPage;
            this.fetchLockList();
        },
        getConfigSymbolRank() {
            service.getConfigSymbolRank()
        },
        // loadContent() {
        //     service.noticeDetail(
        //         this.param1
        //     ).then(rep => {
        //         this.isLoading = false;
        //         this.fundList = rep.data.list;
        //         this.fundTitle = rep.data.list[0].title;
        //         this.fundContent = rep.data.list[0].content;

        //         let detail = 0;
        //         detail.contentHtml = getFormattedContentHtml(detail.content);
        //         this.detail = detail;
        //     });
        // },
        fund() {
            this.param1.type = "FUND";
            this.isActive1 = true;
            this.isActive2 = false;
            this.isActive3 = false;
        },
        report() {
            this.param1.type = "REPORT";
            this.isActive1 = false;
            this.isActive2 = false;
            this.isActive3 = true;
        },
        disclosure() {
            this.param1.type = "PUBLISH";
            this.isActive1 = false;
            this.isActive2 = true;
            this.isActive3 = false;
        },
        seePdf(url, name) {
            window.open(url + name, "_blank");
        },
        getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

    }
});

function getFormattedContentHtml(content) {
    let arrs = content.split('\n');
    let results = [];
    $.map(arrs, (text, index) => {
        text = $.trim(text);
        results.push(`<p class="text">${text}</p>`);
    });
    return results.join('');
}